package org.hncors.gnssparse.fragement;


import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.southgnss.connect.DataLinkerType;
import com.southgnss.connect.DeviceParManage;
import com.southgnss.connect.TopDeviceManage;

import org.hncors.gnssparse.activity.BlueSearchConnectActivity;
import org.hncors.gnssparse.ProgramConfigWrapper;
import org.hncors.gnssparse.R;
import org.hncors.gnssparse.SettingPageRtkManagerNetConfigBluetooth;
import org.hncors.gnssparse.base.BaseFragment;
import org.hncors.gnssparse.util.ToastUtil;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import static android.app.Activity.RESULT_OK;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment {

    private View view;
    @ViewInject(R.id.source_from_tv)
    private TextView sourceFromYv;
    @ViewInject(R.id.ble_setting_tv)
    private TextView bleSettingTv;
    @ViewInject(R.id.connect_setting_tv)
    private TextView connectSettingTv;

    private String strName;
    private boolean connState = false;

    private String strAddress = "";
    private int connectType = 0;//0-本身，1-蓝牙
    private String mUserName = "";
    private static final int PERMISSION_LOCATION = 1;
    public MineFragment() {
        // Required empty public constructor

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(view == null){
            view = super.onCreateView(inflater,container,savedInstanceState);
            appendMainBody(this, R.layout.activity_setting);
            appendTopBody(R.layout.activity_top_icon);
            setTopBarTitle("设置");
            setTopLeftGone();

        }
        initData();
        initUI();
        mActivity.requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_LOCATION);

        return view;

    }
    @Event(value = {R.id.source_from_layout, R.id.ble_setting_layout, R.id.connect_setting_layout},type = View.OnClickListener.class)
    private void onEvenOnclick(View view){
        int id = view.getId();
        Intent intent ;
        switch (id){
            case R.id.source_from_layout:
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setTitle(getString(R.string.main_page_connection_type_tip));
                builder.setSingleChoiceItems(R.array.connectType, connectType, new ConnectTypeSingeChoice());
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            case R.id.ble_setting_layout:
                if (connectType == 0) {
                    ToastUtil.show(mActivity, "请先设置数据来源为蓝牙");
                    return;
                }
                 intent = new Intent(mActivity, BlueSearchConnectActivity.class);
                startActivityForResult(intent, 10);
                break;
            case R.id.connect_setting_layout:
                intent = new Intent(mActivity, SettingPageRtkManagerNetConfigBluetooth.class);
                startActivityForResult(intent, 10);
                break;
        }
    }
    private void initData() {
        connectType = ProgramConfigWrapper.GetInstance(mActivity).GetConnectType();
        strName = ProgramConfigWrapper.GetInstance(mActivity).GetBluetoothName();
        strAddress = ProgramConfigWrapper.GetInstance(mActivity).GetBluetoothAddress();
        if (DeviceParManage.GetInstance() != null) {
            mUserName = DeviceParManage.GetInstance().GetWorkParaUserName();
            ;
        }
      //  TopDeviceManage.getInstance(mActivity).setGnssInfoListener(mActivity);
    }
    private void initUI() {
        sourceFromYv.setText(connectType == 0 ? getString(R.string.local) : getString(R.string.bluetooth));
        //蓝牙相关
        bleSettingTv.setText(strName);
        connectSettingTv.setText(mUserName);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 10 && resultCode == RESULT_OK) {
            strName = data.getStringExtra("BluetoothName");
            strAddress = data.getStringExtra("BluetoothAddress");
            bleSettingTv.setText(strName);
            ProgramConfigWrapper.GetInstance(mActivity).SetBluetoothName(strName);
            ProgramConfigWrapper.GetInstance(mActivity).SetBluetoothAddress(strAddress);


        }
        if (requestCode == 100 && resultCode == RESULT_OK) {
            //            if (data.getBooleanExtra("isClearSateListener", true))
            //                TopDeviceManage.getInstance(this).clearGnssSateInfoListener();

        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    public class ConnectTypeSingeChoice implements DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            connectType = i;
            if (i == 0) {
                sourceFromYv.setText(getString(R.string.local));
            } else if (i == 1) {
                sourceFromYv.setText(getString(R.string.bluetooth));
            }
            ProgramConfigWrapper.GetInstance(mActivity).SetConnectType(connectType);
            dialogInterface.dismiss();
        }
    }


}
